<?php
class Admin_ProductController extends Zendvn_Controller_Action
{
	public function init()
	{
		$template_path=TEMPLATE_PATH."/admin/system";
		$this->loadTemplate($template_path);
		
	}
	public function indexAction()
	{
		
		
		$session=new Admin_Model_Session();
		$this->view->session=($session->getSession()->toArray());
		
		$cate= $this->_request->getParam('lbparent',0);
		$tmp = $this->_request->getParam('lbproduct',0);  
		
		$category = new Admin_Model_Category();
		$this->view->category=($category->getCategory($cate)->toArray());
		
		$product = new Admin_Model_Product();
		$this->view->productses =$product->getProducSes($cate)->toArray();
		
		$this->view->productcat =$product->getProducCat($tmp);
		//$this->_helper->viewRenderer->setNoRender();

	}
	public function addproductAction()
	{
		/*$upload = new Zend_File_Transfer_Adapter_Http();
		$a=$upload->getFileInfo();
		echo "<pre>";
		print_r($a);
		echo "</pre>";*/
		
		$form= new Admin_Form_Product();
		$product=new Admin_Model_Product();
		
		$form->submit->setLabel("add");
		if(is_string($form->image->getFileName()))
		{
			$oldname = pathinfo($form->image->getFileName());
			
			$extend=$oldname['extension'];
						
			$form->image->addFilter("Rename",time().".".$extend);
		}
								  
		$this->view->form=$form;
		if($this->_request->isPost()&& $this->_request->getParam('submit'))
		{
			$formdata= $this->_request->getPost();
		
			if($form->isValid($formdata))
			{
				$image=$form->getValue('image');
	
				$namePro=$form->getValue('namePro');
				$sumary=$form->getValue('textarea3');
				$descripPro=$form->getValue('textarea2');
			
				$idSes=$form->getValue('idSes');
				$idCat=$form->getValue('idCat');
			
				$date=$form->getValue('date');
				$count=$form->getValue('count');
				$price=$form->getValue('price');
				$image=$form->getValue('image');
				$product->addProduct($namePro,$sumary,$descripPro,$idSes,$idCat,$date,$count,$image,$price);
				
				$this->view->tmp=1;
				$this->_helper->redirector('index');
				
			}
			else
			{
				$form->populate($formdata);
			}
			
		}
	}
	public function editproductAction()
	{
		$form= new Admin_Form_Product();
		$product=new Admin_Model_Product();
		$form->submit->setLabel("edit");
		if(is_string($form->image->getFileName()))
		{
			$oldname = pathinfo($form->image->getFileName());

			$extend=$oldname['extension'];
						
			$form->image->addFilter("Rename",time().".".$extend);
		}
	
		$chk=$this->_request->getPost('chk');
		$tmp=explode('|',$chk[0]);
		
		
		$form->getElement('idPro')->setValue($tmp[0]);
		$form->getElement('namePro')->setValue($tmp[1]);
		$form->getElement('idSes')->setValue($tmp[2]);
		$form->getElement('idCat')->setValue($tmp[3]);
		$form->getElement('textarea3')->setValue($tmp[4]);
		$form->getElement('textarea2')->setValue($tmp[5]);
		$form->getElement('count')->setValue($tmp[6]);
		$form->getElement('date')->setValue($tmp[7]);
		$form->getElement('image')->setValue($tmp[8]);
							      
		$form->getElement('hidimage')->setValue($tmp[8]);
		$form->getElement('price')->setValue($tmp[9]);
		
		
		$this->view->form=$form;
		
		if($this->_request->isPost()&& $this->_request->getParam('submit'))
		{
			$formdata= $this->_request->getPost();
		
			if($form->isValid($formdata))
			{			      
				$idPro=$form->getValue('idPro');
				$namePro=$form->getValue('namePro');
				$sumary=$form->getValue('textarea3');
				$descripPro=$form->getValue('textarea2');
			
				$idSes=$form->getValue('idSes');
				$idCat=$form->getValue('idCat');
			
				$date=$form->getValue('date');
				$count=$form->getValue('count');
				$price=$form->getValue('price');
				$form->image->receive();
				$image=$form->getValue('image');
				
				
				
				if($image=='')
				{
					$image=$form->getValue('hidimage');
				}
				$product->editProduct($idPro,$namePro,$sumary,$descripPro,$idSes,$idCat,$date,$count,$image,$price);
				
				$this->view->tmp=1;
				$this->_helper->redirector('index');
				
			}
			else
			{
				$form->populate($formdata);
			}
			
		}
		
			
	}
	public function deleteproductAction()
	{
		$form = new  Admin_Form_Product();
		$product= new Admin_Model_Product();
		$form->submit->setLabel("delete");
		$chk=$this->_request->getPost('chk');
		foreach($chk as $value)
		{
			$tmp[]=explode('|',$value);
	
		}
		foreach ($tmp as $id)
		{
			$product->deleteProduct($id[0]);
		}	
		
		$this->_helper->redirector('index');

		$this->_helper->viewRenderer->setNoRender();
		
		
	}
}